package com.ctsi.sms.service;


import com.ctsi.sms.BaseMessage;
import com.ctsi.sms.codec.smgp.msg.SMGPSubmitMessage;
import com.ctsi.sms.connect.manager.EndpointEntity;
import com.ctsi.sms.connect.manager.EndpointManager;
import com.ctsi.sms.connect.manager.smgp.SMGPClientEndpointEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Component
public class ZzzxSMGPEndPoint implements InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(ZzzxSMGPEndPoint.class);

    public static final String SMS_CLIENT_ID = "zzzx-sms-client";

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("init sms gateway。。。。。。。。。。。。。。。。");
        SMGPEndpointINIT();
    }



    public void SMGPEndpointINIT() throws Exception {

        final EndpointManager manager = EndpointManager.INS;

        SMGPClientEndpointEntity client = new SMGPClientEndpointEntity();
        client.setId(SMS_CLIENT_ID);
        client.setHost("219.146.6.68");
        client.setPort(3058);
        client.setClientID("zhzhzx");
        client.setPassword("zhzhzx20");
        client.setChannelType(EndpointEntity.ChannelType.DUPLEX);

        client.setMaxChannels((short)12);
        client.setRetryWaitTimeSec((short)100);
        client.setUseSSL(false);
        client.setReSendFailMsg(false);
        client.setClientVersion((byte)0x30);
		client.setWriteLimit(200);
		client.setReadLimit(200);
//        List<BusinessHandlerInterface> clienthandlers = new ArrayList<>();
//        clienthandlers.add( new SMGPSessionConnectedHandler(2));
//        client.setBusinessHandlerSet(clienthandlers);
        manager.addEndpointEntity(client);

        manager.openAll();
        logger.info("SMS service startUp.............");
//        LockSupport.park();
//        logger.info("SMS service shutdown.............");
//        EndpointManager.INS.close();
    }

    /**
     * 创建短信
     * @param msgContent 短信内容
     * @param phone 手机号
     * @return
     */
    public static BaseMessage createMessage(String msgContent, String phone) {
        SMGPSubmitMessage pdu = new SMGPSubmitMessage();
        pdu.setSrcTermId("106597881");
        pdu.setDestTermIdArray(phone);
        pdu.setMsgContent(msgContent);
        pdu.setServiceId("20001");
        pdu.setNeedReport(true);
        pdu.setFeeType("00");
        pdu.setFeeCode("0");
        return pdu;
    }

}
